package com.app.shop.activitys;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.app.shop.R;
import com.app.shop.adapters.GoodsListAdapter;
import com.app.shop.beans.GoodsBean;

import com.app.library.base.BaseActivity;
import com.app.library.beans.EventMessage;
import com.app.library.utils.UserManager;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class  GoodsListActivity extends BaseActivity {


    private TextView tvTitle;
    private RecyclerView recy;
    private List<GoodsBean> list = new ArrayList<>();
    private TextView tvAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        tvTitle= (TextView) findViewById(R.id.tv_title);
        recy= (RecyclerView) findViewById(R.id.recy);
        tvAdd= (TextView) findViewById(R.id.tv_add);
        tvAdd.setOnClickListener(this::onViewClicked);

        ImageView imgvreturn= (ImageView) findViewById(R.id.imgv_return);
        imgvreturn.setOnClickListener(this::onViewClicked);
        tvTitle.setText("商品管理");
        tvAdd.setVisibility(View.VISIBLE);
        tvAdd.setText("添加");
        initData();
    }

    private void initData() {
        list.clear();
        List<GoodsBean> list_all = DataSupport.findAll(GoodsBean.class);
        for (int i = 0; i < list_all.size(); i++) {
            if (list_all.get(i).getCreate_user_id().equals(UserManager.getUserId(this))) {
                list.add(list_all.get(i));
            }
        }
        Collections.reverse(list);
        GoodsListAdapter adapter = new GoodsListAdapter(R.layout.item_goods_list, list);
        adapter.setEmptyView(LayoutInflater.from(this).inflate(R.layout.view_list_empty, null));
        recy.setLayoutManager(new LinearLayoutManager(this));
        recy.setAdapter(adapter);

        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("bean", list.get(position));
                bundle.putInt("from", 1);
                showActivity(GoodsListActivity.this, GoodsDetailActivity.class, bundle);
            }
        });
    }



    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.imgv_return:
                onBackPressed();
                break;

            case R.id.tv_add:
                showActivity(this, AddGoodsActivity.class);
                break;
        }
    }

    @Override
    public void onEvent(EventMessage msg) {
        super.onEvent(msg);
        if (msg.getMessageType() == EventMessage.REFRESH) {
            initData();
        }
    }
}
